#ifndef SD_DATAREADERLISTENER_HH_
#define SD_DATAREADERLISTENER_HH_

#include <fastdds/dds/subscriber/DataReaderListener.hpp>
#include <fastdds/dds/subscriber/qos/DataReaderQos.hpp>
#include <fastdds/dds/subscriber/SampleInfo.hpp>

using namespace eprosima::fastdds::dds;

template <class DT_T> 
class SDPltDataReaderListener : public DataReaderListener{
    
private:
    DT_T message_;  
    void(*cb_)(DT_T*);

public:
    SDPltDataReaderListener(void(*cb)(DT_T*)){
        this->cb_=cb;
    }

    void on_subscription_matched(DataReader* reader,const SubscriptionMatchedStatus& info) override{
        if(info.current_count_change==1){
            std::cout<<"Subcriber matched."<<std::endl;
        }else if(info.current_count_change==-1){
            std::cout<<"Subcriber unmatched."<<std::endl;
        }else{
            std::cout<<"Subcriber matching error!"<<std::endl;
        }
    }

    void on_data_available(DataReader* reader) override{
        SampleInfo info;
        if(reader->take_next_sample(&this->message_,&info)==ReturnCode_t::RETCODE_OK){
            if(info.valid_data){           
                this->cb_(&this->message_);                          
            }
        }
    }
};

#endif